package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xd.study.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by DGF on 2015/5/5.
 * 在内存卡中创建文件，并进行读写
 */
public class FileActivity extends Activity {

    private File file;

    //File file = new File("/mnt/ext/sdcard/test");//外部存储
    private String filePath = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file);

        /**
         * 应用程序默认的数据存储目录
         */
        file = getFilesDir();
        Toast.makeText(this, "应用程序默认的数据存储目录为："+file.toString(), Toast.LENGTH_LONG).show();
        /**
         * 应用程序默认的缓存文件存储的路径，可以把不重要的文件在此处创建使用；
         * 当手机内存不足时，会自动删除该目录下的内容
         */
        file = getCacheDir();
        Toast.makeText(this, "应用程序默认的缓存文件存储目录为："+file.toString(), Toast.LENGTH_LONG).show();

        /**
         * 指定权限，创建文件夹
         * /data/data/<包名>/app_imooc
         */
        file = getDir("imooc", MODE_PRIVATE);
        Toast.makeText(this, "自定义目录："+file.toString(), Toast.LENGTH_LONG).show();

        /**
         * 外部存储目录
         */
//        file = getExternalFilesDir(type);

        /**
         * 外部缓存目录
         * app卸载后，会自动清除该目录下的数据
         * 如果开发者不遵守这样的规则（不把数据放到 /data/data/<包名> 或 /mnt/sdcard/Android/data/<包名>），
         * 卸载之后不会自动清除数据，将会造成所谓的数据垃圾
         */
        file = getExternalCacheDir();
        Toast.makeText(this, "外部缓存目录："+file.toString(), Toast.LENGTH_LONG).show();

        filePath = Environment.getExternalStorageDirectory().toString();

        Button btnCreate = (Button)this.findViewById(R.id.file_btn_create);
        Button btnWrite = (Button)this.findViewById(R.id.file_btn_write);
        Button btnRead = (Button)this.findViewById(R.id.file_btn_read);
        Button btnDelete = (Button)this.findViewById(R.id.file_btn_delete);

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                file = new File(filePath, "imooc.txt");
                if (file.exists()){
                    Toast.makeText(FileActivity.this, "文件已经存在", Toast.LENGTH_SHORT).show();
                }else{
                    try{
                        file.createNewFile();
                        Toast.makeText(FileActivity.this, "文件创建成功："+file.toString(), Toast.LENGTH_SHORT).show();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        });

        btnWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = "fate阿塔发分阿哥阿嘎违法俄无法俄无法啊分阿王国维阿嘎嘎嘎问啊aaaa";
                try {
//                    FileOutputStream fos = openFileOutput("imooc.txt", MODE_PRIVATE);
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(text.getBytes());
                    fos.close();
                    Toast.makeText(FileActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(FileActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = null;
                try {
//                    FileInputStream fis = openFileInput("imooc.txt");
                    FileInputStream fis = new FileInputStream(file);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = fis.read(buffer)) != -1){
                        byteArrayOutputStream.write(buffer, 0, len);
                    }
                    text = byteArrayOutputStream.toString();
                    fis.close();
                    Toast.makeText(FileActivity.this, "读取文件成功："+text, Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(FileActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileActivity.this, "文件读取失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file.exists()){
                    file.delete();
                    Toast.makeText(FileActivity.this, "文件删除成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(FileActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
